package controller

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"gowork/customerReplacementProcess/model"
	"strconv"
)

type ReplacementProcessController struct {
	Ctx iris.Context
	CustomerChange *model.CustomerChange
}

func (rpc *ReplacementProcessController)PostApply() mvc.Result {

	rpc.CustomerChange = new(model.CustomerChange)

	var customerChange model.CustomerChange
	rpc.Ctx.ReadJSON(&customerChange)

	res, err := rpc.CustomerChange.Apply(&customerChange)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (rpc *ReplacementProcessController)PostConfirmation() mvc.Result {

	rpc.CustomerChange = new(model.CustomerChange)

	adviserIdStr := rpc.Ctx.FormValue("adviserId")
	adviserId, err := strconv.ParseInt(adviserIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	auditIdStr := rpc.Ctx.FormValue("auditId")
	auditId, err := strconv.ParseInt(auditIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := rpc.CustomerChange.Confirmation(adviserId, auditId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (rpc *ReplacementProcessController)PostReplacement() mvc.Result {

	rpc.CustomerChange = new(model.CustomerChange)

	adviserIdStr := rpc.Ctx.FormValue("adviserId")
	adviserId, err := strconv.ParseInt(adviserIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	auditIdStr := rpc.Ctx.FormValue("auditId")
	auditId, err := strconv.ParseInt(auditIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := rpc.CustomerChange.Replacement(adviserId, auditId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (rpc *ReplacementProcessController)PostQueryautid() mvc.Result {

	rpc.CustomerChange = new(model.CustomerChange)

	adviserIdStr := rpc.Ctx.FormValue("adviserId")
	adviserId, err := strconv.ParseInt(adviserIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := rpc.CustomerChange.QueryAutid(adviserId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":"200",},
	}
}





































